package com.jy.admin.rest.Response;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.jy.admin.db.entity.BillHistoryVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class QueryResponseData implements Serializable {

    private int hasData = 0;

    private Judgement judgement = new Judgement();

    private BillRecord latestBill = new BillRecord();

    private List<BillRecord> allBills = new ArrayList<>();

    public QueryResponseData(List<BillHistoryVo> billHistoryVos) {
        if (CollectionUtils.isEmpty(billHistoryVos)) {
            return;
        }

        hasData = 1;

        billHistoryVos.forEach(billHistoryVo -> {
            int paidStatus = billHistoryVo.getPaidStatus();
            allBills.add(new BillRecord()
                    .setPaidDate(billHistoryVo.getPaidDate())
                    .setOverdueDays(billHistoryVo.getOverdueDays())
                    .setPaidAmount(paidStatus == 1 ? 0 : billHistoryVo.getBorrowAmount())
                    .setPaidStatus(paidStatus == 1 ? 0 : 1)
            );
        });

        judgement = new Judgement().setUserType(getUserType());
        latestBill = allBills.get(0);
        for (BillRecord billRecord : allBills) {
            if (!DateUtil.compareDate(billRecord.getPaidDate(), latestBill.getPaidDate())) {
                latestBill = billRecord;
            }
        }
    }


    private Integer getUserType() {
        Integer userType = 0;

        int maxOverdueDays = allBills.get(0).getOverdueDays();

        for (BillRecord billRecord : allBills) {
            if (billRecord.getOverdueDays() > maxOverdueDays) {
                maxOverdueDays = billRecord.getOverdueDays();
            }
        }

        if (maxOverdueDays > 0) {
            userType = 2;
        } else {
            userType = 1;
        }

        return userType;
    }

    public int getHasData() {
        return hasData;
    }

    public QueryResponseData setHasData(int hasData) {
        this.hasData = hasData;
        return this;
    }

    public Judgement getJudgement() {
        return judgement;
    }

    public QueryResponseData setJudgement(Judgement judgement) {
        this.judgement = judgement;
        return this;
    }

    public BillRecord getLatestBill() {
        return latestBill;
    }

    public QueryResponseData setLatestBill(BillRecord latestBill) {
        this.latestBill = latestBill;
        return this;
    }

    public List<BillRecord> getAllBills() {
        return allBills;
    }

    public QueryResponseData setAllBills(List<BillRecord> allBills) {
        this.allBills = allBills;
        return this;
    }

    private class Judgement {
        private int userType = 0;

        public int getUserType() {
            return userType;
        }

        public Judgement setUserType(int userType) {
            this.userType = userType;
            return this;
        }
    }

    public class BillRecord {
        private int paidAmount = 0;
        private int paidStatus = 0;
        private String paidDate = "";
        private int overdueDays = -99;

        public int getPaidAmount() {
            return paidAmount;
        }

        public BillRecord setPaidAmount(int paidAmount) {
            this.paidAmount = paidAmount;
            return this;
        }

        public int getPaidStatus() {
            return paidStatus;
        }

        public BillRecord setPaidStatus(int paidStatus) {
            this.paidStatus = paidStatus;
            return this;
        }

        public String getPaidDate() {
            return paidDate;
        }

        public BillRecord setPaidDate(String paidDate) {
            this.paidDate = paidDate;
            return this;
        }

        public int getOverdueDays() {
            return overdueDays;
        }

        public BillRecord setOverdueDays(int overdueDays) {
            this.overdueDays = overdueDays;
            return this;
        }
    }
}
